home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 937 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.5 KB

  1. Path: dawn.mmm.com!news
  2. From: kjhopps@mmm.com (Kevin J Hopps)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Constructor Exceptions
  5. Date: 8 Jan 1996 15:25:17 GMT
  6. Organization: 3M - St. Paul, MN  55144-1000 US
  7. Message-ID: <4crd0t$9oh@dawn.mmm.com>
  8. References: <4bud9g$pv5@oxy.rust.net> <4cbhcl$kst@dawn.mmm.com> <4ce68n$8u4@sundog.tiac.net> <4ch1c3$bsm@oxy.rust.net>
  9. Reply-To: kjhopps@mmm.com
  10. X-Newsreader: TIN [version 1.2 PL2]
  11.  
  12. Paul Gunn (pgunn@mail.cbf.com) wrote:
  13. > Here is an excerpt from the Visual C++ 2.1 documentation. It seems to
  14. > indicate that 'this' will not be cleaned up.
  15.  
  16. > >
  17. > Throwing an exception in a constructor is tricky, however, because the
  18. > memory for the object itself has already been allocated by the time
  19. > the constructor is called. There is no simple way to deallocate the
  20. > memory occupied by the object from within the constructor for that
  21. > object. Thus, you will find that throwing an exception in a
  22. > constructor will result in the object remaining allocated. For a
  23. > discussion of how to detect objects in your program that have not been
  24. > deallocated, see the article Diagnostics: Detecting Memory Leaks. 
  25. > If you are performing operations in your constructor that can fail, it
  26. > might be a better idea to put those operations into a separate
  27. > initialization function rather than throwing an exception in the
  28. > constructor. That way, you can safely construct the object and get a
  29. > valid pointer to it. Then, you can call the initialization function
  30. > for the object. If the initialization function fails, you can delete
  31. > the object directly. 
  32. > <
  33.  
  34. > What do you think?
  35.  
  36. From the ANSI C++ Draft dated April 28, 1995 section 5.3.4 [expr.new],
  37. paragraph 17:
  38.     If the constructor throws an exception and the new-expression does not
  39.     contain   a   new-placement,   then    the    deallocation    function
  40.     (_basic.stc.dynamic.deallocation_,  _class.free_)  is used to free the
  41.     memory in which the object was  being  constructed,  after  which  the
  42.     exception continues to propagate in the context of the new-expression.
  43.  
  44. The standard makes no provisions for problems which have "no simple way" of
  45. being solved :-)
  46. --
  47. Kevin J. Hopps                  e-mail: kjhopps@mmm.com
  48. 3M Company                      phone:  (612) 737-4643
  49. 3M Center, Bldg. 235-2D-57      fax:    (612) 737-2700
  50. St. Paul, MN 55144-1000         Opinions are my own.  I don't speak for 3M.
  51.     But 3M speaks for me -- I did not write the following line:
  52.  
  53. Opinions expressed herein are my own and may not represent those of 3M.
  54.